package yz.dut.edu.graduate.service;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yz.dut.edu.graduate.bean.Result;
import yz.dut.edu.graduate.pojo.Dish;
import yz.dut.edu.graduate.pojo.Dish_Ingredient;

import java.util.List;

@Service
@Transactional(rollbackFor = RuntimeException.class)
public class ComprehensiveService {
    @Autowired
    DishService dishService;

    @Autowired
    IngredientService ingredientService;

    @Autowired
    Dish_IngredientService dish_ingredientService;

    public Result DishAndIngredientInsert(Dish dish,String [] ingredient){

        Result result= dishService.Push(dish);
        if(!result.isSuccess()){
            return result;
        }
        for(String s:ingredient){
            if(s==""){
                continue;
            }
            int i_id;
            try {
                i_id = ingredientService.GetIid(s);
            }catch (Exception e){
                result.setMsg("没有这种原材料");
                result.setSuccess(false);
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
            Dish_Ingredient di = new Dish_Ingredient();
            di.setdId((Integer) result.getDetails());
            di.setiId(i_id);
            dish_ingredientService.Push(di);
        }
        return result;
    }

    public Result DishAndIngredientDelete(String dishName){
        int d_id=dishService.SearchByDishName(dishName);
        Result result=new Result();
        //没找到菜
        if(d_id==-1){
            String msg="数据库中没有"+dishName+"这个条目";
            result.setMsg(msg);
        }else{
            result.setDetails(dish_ingredientService.DeleteDishIngredient(d_id));
            result.setSuccess(dishService.DeleteDish(d_id));
            result.setMsg("删除成功");
        }
        return result;
    }

    public Result DishPriceOff(String dishName,float off){
        int d_id=dishService.SearchByDishName(dishName);
        Result result=new Result();
        if(d_id==-1){
            String msg="数据库中没有"+dishName+"这个条目";
            result.setMsg(msg);
        }else{
            int rows=dishService.DishOff(d_id,off);
            result.setMsg("状态更新完毕");
            result.setSuccess(true);
        }
        return result;
    }
    public Result DishPriceUpdate(String dishName,int price,int state){
        int d_id=dishService.SearchByDishName(dishName);
        Result result=new Result();
        if(d_id==-1){
            String msg="数据库中没有"+dishName+"这个条目";
            result.setMsg(msg);
        }else{
            int rows=dishService.DishPriceAndOff(d_id,price,state);
            result.setMsg("状态更新完毕");
            result.setSuccess(true);
        }
        return result;
    }

    public List<Dish> SelectCertain(String dishName){
        return dishService.SelectByDishName(dishName);
    }

    public List<Dish> SelectAll(){
        return dishService.SelectAll();
    }

    public PageInfo<Dish> SelectPage(int pageNum, int pageSize){
        return dishService.SelectPage(pageNum,pageSize);
    }


}
